package org.zwy.gradetrack.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.zwy.gradetrack.common.Result;
import org.zwy.gradetrack.entity.GraduationRequirement;
import org.zwy.gradetrack.entity.Indicator;
import org.zwy.gradetrack.service.IndicatorService;
import org.zwy.gradetrack.service.GraduationRequirementService;

import java.util.List;

/**
 * 指标点管理控制器
 */
@RestController
@RequestMapping("/api/indicators")
public class IndicatorController {

    @Autowired
    private IndicatorService indicatorService;

    @Autowired
    private GraduationRequirementService graduationRequirementService;

    /**
     * 获取指标点列表
     */
    @GetMapping
    @PreAuthorize("hasAnyRole('SYSTEM_ADMIN', 'TEACHER','GRADUATE')")
    public Result<List<Indicator>> getIndicatorList(@RequestParam(required = false) Long requirementId) {
        List<Indicator> indicators;
        if (requirementId != null) {
            // 检查毕业要求是否存在
            if (!graduationRequirementService.lambdaQuery()
                    .eq(GraduationRequirement::getId, requirementId)
                    .exists()) {
                return Result.error(404, "毕业要求不存在");
            }
            
            indicators = indicatorService.lambdaQuery()
                    .eq(Indicator::getRequirementId, requirementId)
                    .list();
        } else {
            indicators = indicatorService.list();
        }
        return Result.success(indicators);
    }

    /**
     * 创建指标点
     */
    @PostMapping
    @PreAuthorize("hasAnyRole('SYSTEM_ADMIN', 'TEACHER')")
    public Result<Indicator> createIndicator(@RequestBody Indicator indicator) {
        // 检查毕业要求是否存在
        if (!graduationRequirementService.lambdaQuery()
                .eq(GraduationRequirement::getId, indicator.getRequirementId())
                .exists()) {
            return Result.error(404, "毕业要求不存在");
        }

        // 检查指标点代码是否已存在
        if (indicatorService.lambdaQuery()
                .eq(Indicator::getIndicatorCode, indicator.getIndicatorCode())
                .exists()) {
            return Result.error(400, "指标点代码已存在");
        }

        // 保存指标点信息
        indicatorService.save(indicator);
        return Result.success(indicator);
    }

    /**
     * 更新指标点
     */
    @PutMapping("/{id}")
    @PreAuthorize("hasAnyRole('SYSTEM_ADMIN', 'TEACHER')")
    public Result<Indicator> updateIndicator(@PathVariable Long id, @RequestBody Indicator indicator) {
        // 检查指标点是否存在
        if (!indicatorService.lambdaQuery()
                .eq(Indicator::getId, id)
                .exists()) {
            return Result.error(404, "指标点不存在");
        }

        // 检查毕业要求是否存在
        if (!graduationRequirementService.lambdaQuery()
                .eq(GraduationRequirement::getId, indicator.getRequirementId())
                .exists()) {
            return Result.error(404, "毕业要求不存在");
        }

        // 检查指标点代码是否重复
        if (indicatorService.lambdaQuery()
                .eq(Indicator::getIndicatorCode, indicator.getIndicatorCode())
                .ne(Indicator::getId, id)
                .exists()) {
            return Result.error(400, "指标点代码已存在");
        }

        // 更新指标点信息
        indicator.setId(id);
        indicatorService.updateById(indicator);
        return Result.success(indicator);
    }

    /**
     * 删除指标点
     */
    @DeleteMapping("/{id}")
    @PreAuthorize("hasAnyRole('SYSTEM_ADMIN', 'TEACHER')")
    public Result<Void> deleteIndicator(@PathVariable Long id) {
        // 检查指标点是否存在
        if (!indicatorService.lambdaQuery()
                .eq(Indicator::getId, id)
                .exists()) {
            return Result.error(404, "指标点不存在");
        }

        // 删除指标点
        indicatorService.removeById(id);
        return Result.success();
    }

    /**
     * 获取指标点详情
     */
    @GetMapping("/{id}")
    @PreAuthorize("hasAnyRole('SYSTEM_ADMIN', 'TEACHER')")
    public Result<Indicator> getIndicatorDetail(@PathVariable Long id) {
        Indicator indicator = indicatorService.getById(id);
        if (indicator == null) {
            return Result.error(404, "指标点不存在");
        }
        return Result.success(indicator);
    }
} 